//
//  CoursesViewController.m
//  KeepProject
//
//  Created by amos on 16/1/17.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "Course.h"
#import "CourseCell.h"
#import "CoursePlanListViewController.h"
#import "CoursesViewController.h"
#import "UIBarButtonItem+Custom.h"
#import "UIView+UIFrame.h"

@interface CoursesViewController ()

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout* flowLayout;
@property (strong, nonatomic) NSArray* courses;
@property (copy, nonatomic) NSString* sourceURL;
@end

@implementation CoursesViewController

- (void)viewWillAppear:(BOOL)animated
{
    //1.设置 Item 间距
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;

    //2.设置 item 大小
    _flowLayout.itemSize = CGSizeMake(self.view.width * 0.5, self.view.width * 0.5);
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //self.navigationController.navigationBar.hidden = YES;

    //1.设置 title
    self.title = @"训练课程";

    //2.设置leftBarItem
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWith:@"  返回" imageName:@"arrow_back_white" target:self action:@selector(pop)];
}
//pop 到上一个界面
- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{

    return 1;
}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.courses.count - 1;
}

//返回 header 视图
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{

    CourseCell* header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.course = self.courses[indexPath.row];

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerClick:)];

    [header addGestureRecognizer:tap];

    return header;
}

//header 点击事件
- (void)headerClick:(UITapGestureRecognizer*)tap
{
    CourseCell* cell = (CourseCell*)tap.view;
    self.sourceURL = cell.course.contentURL;

    [self performSegueWithIdentifier:@"goToPlan" sender:cell];
}

// 返回 cell 视图
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString* reuseIdentifier = @"CourseCell";

    CourseCell* cell = nil;
    if (indexPath.row == self.courses.count - 2) {
        reuseIdentifier = @"lastCell";
    }
    else {
        reuseIdentifier = @"CourseCell";
    }
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // Configure the cell
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256) / (float)255 green:arc4random_uniform(256) / (float)255 blue:arc4random_uniform(256) / (float)255 alpha:1];

    cell.course = self.courses[indexPath.row + 1];

    return cell;
}

//1.跳转到具体 plan 界面前,准备一下数据
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{

    //0.获取点中了哪一个 cell
    NSIndexPath* indexPath = self.collectionView.indexPathsForSelectedItems.lastObject;

    Course* course = self.courses[indexPath.row + 1];
    ;
    self.sourceURL = course.contentURL;

    //1.判断跳转到哪一个界面
    UIViewController* vc = segue.destinationViewController;

    if ([vc isKindOfClass:[CoursePlanListViewController class]]) {
        CoursePlanListViewController* tempVc = (CoursePlanListViewController*)vc;
        NSLog(@"before:%@", self.sourceURL);
        tempVc.sourceURL = self.sourceURL;
    }
}

#pragma mark - 懒加载
- (NSArray*)courses
{
    if (!_courses) {
        _courses = [Course coursesWithPlist];
    }
    return _courses;
}

@end
